package bold.module.rabbitmq;

import com.rabbitmq.client.*;

import java.io.IOException;
import java.nio.charset.StandardCharsets;

/**
 * @date 2019-05-28
 * @auther likailun
 **/
public class Consumer {

    public static void main(String[] argv) throws Exception {
        final Channel channel = Producer.getConnection().createChannel();
        channel.exchangeDeclare(Constant.exchange_name, BuiltinExchangeType.DIRECT);
        channel.queueBind(Constant.queue_name, Constant.exchange_name, Constant.binding_key);
        System.out.println(" ----------------------");

        // 创建队列消费者
        DefaultConsumer defaultConsumer = new DefaultConsumer(channel) {
            @Override
            public void handleDelivery(String consumerTag, Envelope envelope, AMQP.BasicProperties properties, byte[] body) throws IOException {
                String message = new String(body, StandardCharsets.UTF_8);
                if (message.equals("1")) {
                    try {
                        Thread.sleep(5000L);
                    } catch (InterruptedException  e) {
                        e.printStackTrace();
                    }
                }
                System.out.println(message);
                channel.basicAck(envelope.getDeliveryTag(), true);
                
            }
        };
        channel.basicConsume(Constant.queue_name, false, defaultConsumer);
    }

}
